package org.theonept.travian.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.theonept.travian.util.HttpUtil;

public class Village extends Entity implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8841065657403467040L;

	private String nomVillage;

	private String idVillage;
	private String positionX;
	private String positionY;
	private Integer nbUnderAttack;
	private Integer nbAttack;
	private Integer nbUnderAssistance;
	private Integer nbAssistance;
	private Boolean heroAdventure;
	private Production production;
	private String loyaute;

	private List<Construction> constructions;
	private Compte compte;

	public String getNomVillage() {
		return nomVillage;
	}

	public void setNomVillage(String nomVillage) {
		this.nomVillage = nomVillage;
	}

	public String getIdVillage() {
		return idVillage;
	}

	public void setIdVillage(String idVillage) {
		this.idVillage = idVillage;
	}

	public Production getProduction() {
		return getProduction(production == null || !production.isUpToDate());
	}

	public Production getProduction(boolean forceUpdate) {
		if (forceUpdate) {
			production = Production.getProduction(this);
			production.update();
		}
		return production;
	}

	public void setProduction(Production production) {

		this.production = production;
	}

	public static List<Village> getVillages(String url, Compte compte) {
		List<Village> villages = new ArrayList<Village>();
		Document document = HttpUtil.getDocumentFromUrl(url);
		List<Element> activeElement = document.getElementById("sidebarBoxVillagelist")
				.getElementsByAttributeValueContaining("href", "?newdid=");
		for (Element element : activeElement) {
			Village village = new Village();
			village.setNomVillage(element.getElementsByAttributeValue("class", "name").first().text());
			village.setIdVillage(element.attr("href").replace("?newdid=", "").replace("&", ""));
			village.setPositionX(element.getElementsByAttributeValue("class", "coordinateX").first().text()
					.replace("(", ""));
			village.setPositionY(element.getElementsByAttributeValue("class", "coordinateY").first().text()
					.replace(")", ""));

			village.setCompte(compte);

			villages.add(village);

		}

		return villages;
	}

	public String getPositionX() {
		return positionX;
	}

	public void setPositionX(String positionX) {
		this.positionX = positionX;
	}

	public String getPositionY() {
		return positionY;
	}

	public void setPositionY(String positionY) {
		this.positionY = positionY;
	}

	public Compte getCompte() {
		return compte;
	}

	public void setCompte(Compte compte) {
		this.compte = compte;
	}

	public void upRessourceAuto() {

		List<Ressource> ressources = getProduction().getRessources();
		Collections.sort(ressources, new ComparatorRessourceUp());
		for (Ressource ressource : ressources) {
			if (ressource.isUpable()) {
				ressource.up();
				break;
			}
		}

	}

	/**
	 * @return the nbUnderAttack
	 */
	public Integer getNbUnderAttack() {
		return nbUnderAttack;
	}

	/**
	 * @param nbUnderAttack
	 *            the nbUnderAttack to set
	 */
	public void setNbUnderAttack(Integer nbUnderAttack) {
		this.nbUnderAttack = nbUnderAttack;
	}

	/**
	 * @return the nbAttack
	 */
	public Integer getNbAttack() {
		return nbAttack;
	}

	/**
	 * @param nbAttack
	 *            the nbAttack to set
	 */
	public void setNbAttack(Integer nbAttack) {
		this.nbAttack = nbAttack;
	}

	/**
	 * @return the nbUnderAssistance
	 */
	public Integer getNbUnderAssistance() {
		return nbUnderAssistance;
	}

	/**
	 * @param nbUnderAssistance
	 *            the nbUnderAssistance to set
	 */
	public void setNbUnderAssistance(Integer nbUnderAssistance) {
		this.nbUnderAssistance = nbUnderAssistance;
	}

	/**
	 * @return the nbAssistance
	 */
	public Integer getNbAssistance() {
		return nbAssistance;
	}

	/**
	 * @param nbAssistance
	 *            the nbAssistance to set
	 */
	public void setNbAssistance(Integer nbAssistance) {
		this.nbAssistance = nbAssistance;
	}

	/**
	 * @return the heroAdventure
	 */
	public Boolean getHeroAdventure() {
		return heroAdventure;
	}

	/**
	 * @param heroAdventure
	 *            the heroAdventure to set
	 */
	public void setHeroAdventure(Boolean heroAdventure) {
		this.heroAdventure = heroAdventure;
	}

	/**
	 * @return the loyaute
	 */
	public String getLoyaute() {
		return loyaute;
	}

	/**
	 * @param loyaute
	 *            the loyaute to set
	 */
	public void setLoyaute(String loyaute) {
		this.loyaute = loyaute;
	}

	/**
	 * @return the constructions
	 */
	public List<Construction> getConstructions() {
		return constructions;
	}

	/**
	 * @param constructions
	 *            the constructions to set
	 */
	public void setConstructions(List<Construction> constructions) {
		this.constructions = constructions;
	}

}
